var app = getApp();

Page({
    data: {
        imgPath: app.getImgUrl(),
        navh: 0,
        animationData: {},
        count: 0,
        turning: !1,
        awardsList: {},
        isHongbao: !1,
        showShareShade: !1,
        showNowinshade: !1,
        showWinshade: !1,
        win_message: "",
        img: "",
        hidden_canvas: !0,
        awards: [],
        prize: 0,
        in_users: 0,
        rotarydraw: [],
        rotarydraw_sum: 0,
        page: 1,
        lastPage: !1,
        poster: "",
        goods: [],
        videoAd: !1,
        video_ad_code: !1,
        from_uid: 0,
        share_Rotary_coin: 0
    },
    initVideoAd: function() {
        var t = this;
        !1 === t.data.videoAd && wx.createRewardedVideoAd && app.globalData.setting.video_ad_code && (t.data.videoAd = wx.createRewardedVideoAd({
            adUnitId: "adunit-" + app.globalData.setting.video_ad_code
        }), t.data.videoAd.onLoad(function() {
            console.log("Video_AD Load OK...");
        }), t.data.videoAd.onClose(function(a) {
            console.log("Video_AD Close OK ..."), console.log(a), a && a.isEnded || void 0 === a ? (console.log("watch OK ..."), 
            app.util.request({
                url: "entry/wxapp/AddSpeedCount",
                data: {
                    num: 1
                },
                success: function(a) {
                    t.data.count++, t.setData({
                        count: t.data.count
                    }), wx.showToast({
                        title: "再抽一次",
                        icon: "success"
                    });
                }
            })) : (console.log("noWatch OK ..."), wx.showModal({
                title: "提示",
                content: "视频没看完，看完视频才能再抽奖哦~~~",
                showCancel: !0,
                confirmColor: "#f43f6c",
                confirmText: "继续观看",
                cancelColor: "#bbb",
                success: function(a) {
                    a.confirm && t.data.videoAd && t.data.videoAd.show();
                },
                fail: function(a) {
                    console.log(a, "showModalErr");
                }
            }));
        }), t.data.videoAd.onError(function(a) {
            console.log(a, "VieoERR....");
        }));
    },
    toGoodsPage: function(a) {
        console.log(a);
        var t = a.currentTarget.dataset.goods_id, o = a.currentTarget.dataset.goods_sign;
        void 0 !== a.currentTarget.dataset.source ? (a = "&source=" + a.currentTarget.dataset.source, 
        o && (a += "&goods_sign=" + o), wx.navigateTo({
            url: "../goods/goods?goods_id=" + t + a
        })) : (o && (t += "&goods_sign=" + o), wx.navigateTo({
            url: "../goods/goods?goods_id=" + t
        }));
    },
    getRotaryInfo: function() {
        var t = 0 < arguments.length && void 0 !== arguments[0] && arguments[0], o = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "cover", e = this;
        app.util.request({
            url: "entry/wxapp/getRotaryInfo",
            data: {
                page: e.data.page
            },
            success: function(a) {
                if (console.log(a.data.data), "function" == typeof t && t(), "append" == o) return a.data.data.rotarydraw.length ? (e.data.rotarydraw = e.data.rotarydraw.concat(a.data.data.rotarydraw), 
                e.setData({
                    rotarydraw: e.data.rotarydraw
                }), !1) : !(e.data.lastPage = !0);
                a = a.data.data, e.setData({
                    count: a.amount,
                    awards: a.info,
                    theory_sum: a.theory_sum.theory_sum,
                    in_users: a.in_users.num,
                    rotarydraw_sum: a.rotarydraw_sum,
                    rotarydraw: a.rotarydraw
                }), e.createRotary();
            }
        });
    },
    loginAfter: function(a) {
        app.loginAfter(a);
    },
    loginCancel: function() {
        app.loginCancel();
    },
    onLoad: function(a) {
        var t = this;
        wx.getSystemInfo({
            success: function(a) {
                t.setData({
                    width: a.windowWidth,
                    height: a.windowHeight
                });
            }
        }), a = a.from_uid || 0, t.setData({
            navH: app.globalData.navHeight,
            video_ad_code: app.globalData.setting.video_ad_code,
            share_Rotary_coin: app.globalData.setting.share_Rotary_coin,
            from_uid: a
        }), t.data.video_ad_code && "devtools" != app.platform && t.initVideoAd(), t.getRotaryInfo();
    },
    createRotary: function() {
        var t = this;
        wx.getSystemInfo({
            success: function(a) {
                t.setData({
                    contentHeight: a.windowHeight
                }), a.windowWidth < 360 ? t.setData({
                    scale: .9
                }) : 500 < a.windowWidth && t.setData({
                    scale: 1.4
                });
            }
        }), app.awardsConfig = {
            count: t.data.count,
            awards: t.data.awards,
            award_c: [ {
                color: "#754805"
            }, {
                color: "#BE9B6D"
            }, {
                color: "#754805"
            }, {
                color: "#BE9B6D"
            }, {
                color: "#EF1611"
            }, {
                color: "#BE9B6D"
            } ]
        }, t.setData({
            count: app.awardsConfig.count
        });
        for (var a = app.awardsConfig.awards, o = a.length, e = 360 / o / 2 + 90, s = [], n = 1 / o, i = wx.createContext("lotteryCanvas"), r = 0; r < o; r++) {
            var d = .38 * t.data.width;
            i.save(), i.beginPath(), i.translate(150, 150), i.moveTo(0, 0), i.rotate((360 / o * r - e) * Math.PI / 180), 
            i.arc(0, 0, d, 0, 2 * Math.PI / o, !1), r % 2 == 0 ? i.setFillStyle("#FFD394") : i.setFillStyle("#FFF1DC"), 
            i.fill(), i.setLineWidth(.5), i.setStrokeStyle("rgba(255,255,255,1)"), i.stroke(), 
            i.restore(), s.push({
                turn: r * n + "turn",
                lineTurn: r * n + n / 2 + "turn",
                award: a[r].station,
                img: a[r].img,
                color: app.awardsConfig.award_c[r].color,
                bg: a[r].bg
            });
        }
        t.setData({
            awardsList: s
        });
    },
    navBack: function() {
        0 < this.data.from_uid ? wx.switchTab({
            url: "../index/index"
        }) : wx.navigateBack();
    },
    toIndex: function() {
        wx.switchTab({
            url: "../index/index"
        });
    },
    onShowShareShade: function() {
        var t;
        app.globalData.userInfo ? ((t = this).setData({
            showShareShade: !0
        }), app.util.request({
            url: "entry/wxapp/getRotaryPoster",
            success: function(a) {
                a = a.data.data, t.setData({
                    poster: a
                });
            }
        })) : app.checkLogin();
    },
    saveQrcodeToPhotosAlbum: function(a) {
        wx.getImageInfo({
            src: this.data.poster,
            success: function(a) {
                console.log(a), wx.saveImageToPhotosAlbum({
                    filePath: a.path,
                    success: function(a) {
                        wx.showToast({
                            title: "已保存至相册",
                            icon: "success"
                        });
                    },
                    fail: function(a) {
                        console.log(a);
                    }
                });
            }
        }), this.chooseShareShade();
    },
    chooseShareShade: function() {
        this.setData({
            showShareShade: !1
        });
    },
    getMoveAbout: function() {
        var t = this;
        app.util.request({
            url: "entry/wxapp/getMoveAbout",
            data: {
                prize: t.data.prize,
                prize_thank: t.data.prize_thank,
                prize_again: t.data.prize_again
            },
            success: function(a) {
                if (console.log(a.data.data), t.setData({
                    goods: a.data.data
                }), "ok" == a.data.message) if (0 == (a = t.data.awardIndex)) t.setData({
                    showNowinshade: !0,
                    win_message: "很遗憾，你与奖品擦肩而过"
                }); else if (1 == a) t.setData({
                    showWinshade: !0,
                    win_message: "恭喜你获得0.1元现金红包"
                }); else if (2 == a) t.setData({
                    showWinshade: !0,
                    win_message: "恭喜你获得1元现金红包"
                }); else {
                    if (3 == a) return void t.setData({
                        showNowinshade: !0,
                        count: t.data.count + 1,
                        win_message: "别遗憾，再送你一次机会"
                    });
                    4 == a ? t.setData({
                        showWinshade: !0,
                        win_message: "恭喜你获得10元现金红包"
                    }) : 5 == a && t.setData({
                        showWinshade: !0,
                        win_message: "恭喜你获得0.01元现金红包"
                    });
                }
                t.getRandomWinLists();
            }
        });
    },
    getRandomWinLists: function() {
        var t = this;
        app.util.request({
            url: "entry/wxapp/getRandomWinLists",
            success: function(a) {
                console.log(""), t.setData({
                    rotarydraw: a.data.data,
                    page: 1,
                    lastPage: !1
                });
            }
        });
    },
    getLottery: function() {
        if (app.globalData.userInfo) {
            var t = this;
            if (t.data.count <= 0) t.data.video_ad_code ? wx.showModal({
                title: "提示",
                content: "没有抽奖次数了，观看视频再转一次",
                showCancel: !0,
                confirmColor: "#f43f6c",
                cancelColor: "#bbb",
                confirmText: "观看视频",
                cancelText: "明天再来",
                success: function(a) {
                    a.confirm && t.data.videoAd && t.data.videoAd.show();
                }
            }) : wx.showToast({
                title: "机会用光了,请明天再来哦",
                icon: "none",
                duration: 2e3
            }); else {
                t.setData({
                    count: t.data.count - 1,
                    turning: !0
                }), setTimeout(function() {
                    t.setData({
                        turning: !1
                    });
                }, 4500);
                for (var a = app.awardsConfig, o = 0, e = t.data.awards, s = t.data.theory_sum, n = new Array(s), i = new Array(), r = 0; r < e.length; r++) {
                    n = [];
                    for (var d = 0; d < e[r].theory; d++) n[d] = e[r].id;
                    i = i.concat(n), n = [];
                }
                console.log("需要生成的数的总个数:" + s), console.log(i);
                var c = i, u = Math.random() * (s - 1) >>> 0;
                console.log("结果为:" + c[u] + "号奖品"), c[u] == t.data.awards[0].id ? o = 0 : c[u] == t.data.awards[1].id ? o = 1 : c[u] == t.data.awards[2].id ? o = 2 : c[u] == t.data.awards[3].id ? o = 3 : c[u] == t.data.awards[4].id ? o = 4 : c[u] == t.data.awards[5].id && (o = 5), 
                console.log("奖品序号：" + o + 1), s = t.data.awards[o].id, c = t.data.awards[0].id, 
                u = t.data.awards[3].id, t.setData({
                    prize: s,
                    prize_thank: c,
                    prize_again: u,
                    awardIndex: o
                }), app.runDegs = app.runDegs || 0, app.runDegs = app.runDegs + (360 - app.runDegs % 360) + (1440 - 60 * o), 
                console.log("deg", app.runDegs), u = wx.createAnimation({
                    duration: 4e3,
                    timingFunction: "ease"
                }), (t.animationRun = u).rotate(app.runDegs).step(), t.setData({
                    animationData: u.export()
                }), (u = wx.getStorageSync("winAwards") || {
                    data: []
                }).data.push(a.awards[o].station + "1个"), wx.setStorageSync("winAwards", u), setTimeout(function() {
                    t.getMoveAbout();
                }, 4e3);
            }
        } else app.checkLogin();
    },
    onReady: function(a) {},
    togglePopup: function() {
        this.setData({
            isShare: !this.data.isShare
        });
    },
    togglePopup2: function() {
        this.setData({
            isHongbao: !this.data.isHongbao
        });
    },
    togglePopup3: function() {
        this.setData({
            isMini: !this.data.isMini
        });
    },
    chooseWinshade: function() {
        this.setData({
            showNowinshade: !1,
            showWinshade: !1
        });
    },
    nomove: function() {},
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        this.data.page = 1, this.data.lastPage = !1, this.getRotaryInfo(function() {
            wx.stopPullDownRefresh();
        });
    },
    onReachBottom: function() {
        if (!0 === this.data.lastPage) return !1;
        this.data.page++, this.getRotaryInfo("", "append");
    },
    onShareAppMessage: function() {
        this.chooseShareShade();
        var a = app.globalData.setting.share_Rotary_title || "快点来抽现金,一般人我不告诉他!", t = app.globalData.setting.share_Rotary_pic || res.target.dataset.src;
        return {
            title: a,
            path: "/first_duoduoke/pages/turntable/turntable" + (void 0 !== app.globalData.userInfo.uid ? "?from_uid=" + app.globalData.userInfo.uid + "&from_act=share" : ""),
            imageUrl: t,
            success: function(a) {
                "shareAppMessage:ok" == a.errMsg ? wx.showToast({
                    title: "分享成功",
                    icon: "success"
                }) : wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            },
            fail: function(a) {
                "shareAppMessage:fail cancel" == a.errMsg || a.errMsg, wx.showToast({
                    title: "分享失败",
                    icon: "none"
                });
            }
        };
    }
});